package icom.acm.manager;
/**
 * @author shench
 * 用户关系管理类
 */
import icom.acm.bean.T_USERRELATION;
import icom.acm.util.DBConnect;

import java.util.ArrayList;
import java.util.List;

import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;

public class T_UserRelationMgr extends Manager{
	public List<T_USERRELATION> selectByU_UED(T_USERRELATION ur, DBConnect db) throws Exception{
		List<T_USERRELATION> list = new ArrayList<T_USERRELATION>();
		String sql = "SELECT UR_ID, UR_U_ID, UR_UED_ID, UR_DATE " +
				"FROM T_USERRELATION WHERE " +
				"UR_U_ID="+ur.getUR_U_ID()+" AND " +
				"UR_UED_ID="+ur.getUR_UED_ID();
		db.prepareStatement(sql);
		db.excuteQuery();
		while (db.next()){
			T_USERRELATION e = new T_USERRELATION(db);
			list.add(e);
		}
		return list;
	}

	/**
	 * 添加一条用户关系
	 * @param ur
	 */
	public boolean insert(T_USERRELATION ur, DBConnect db) {
		String sql = "INSERT INTO T_USERRELATION VALUES(NULL, "+ur.getUR_U_ID()+", "+ur.getUR_UED_ID()+", NOW())";
		
		db.prepareStatement(sql);
		try {
			db.excute();
		} catch (MySQLIntegrityConstraintViolationException e) {
			e.printStackTrace();
			return false;
		} finally{		
			db.close();
		}
		return true;		
	}

	public List<T_USERRELATION> selectByUedId(Integer m_u_id, DBConnect db) throws Exception {
		String sql = "SELECT UR_ID, UR_U_ID, UR_UED_ID, UR_DATE" +
				" FROM T_USERRELATION WHERE UR_UED_ID="+m_u_id;
		int cnt = count("SELECT COUNT(*) FROM " +
				"T_USERRELATION WHERE UR_UED_ID="+m_u_id, db);
		List<T_USERRELATION> list = new ArrayList<T_USERRELATION>(cnt);		
		db.prepareStatement(sql);
		db.excuteQuery();
		while (db.next()){
			T_USERRELATION e = new T_USERRELATION(db);
			list.add(e);
		}
		return list;
	}
}
